<?php

namespace App\Containers\AppSection\File\UI\API\Requests;

use App\Ship\Parents\Requests\Request;
use Illuminate\Validation\Rule;

class CreateFileRequest extends Request
{
    /**
     * Define which Roles and/or Permissions has access to this request.
     */
    protected array $access = [
        'permissions' => '',
        'roles' => '',
    ];

    /**
     * Id's that needs decoding before applying the validation rules.
     */
    protected array $decode = [
        // 'id',
    ];

    /**
     * Defining the URL parameters (e.g, `/user/{id}`) allows applying
     * validation rules on them and allows accessing them like request data.
     */
    protected array $urlParameters = [
        // 'id',
    ];

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        $projectRule = ['project_id' => ['required', Rule::exists('projects', 'id')->where(function ($query) {
            return $query->where('profile_id', $this->profile()->id);
        })]];
        switch ($this->request->get('file_type', '')) {
            case 'passport':
            case 'company':
            {
                $data = config('appSection-file.image');
                break;
            }
            case 'project_company':
            {
                $data = array_merge($projectRule, config('appSection-file.image'));
                break;
            }

            case 'legal_form_documents':
            case 'questionnaires':
            case 'qualification' :
            {
                $data = config('appSection-file.document');
                break;
            }

            case 'settings' :
            {
                $data = config('appSection-file.doc');
                break;
            }

            case 'project_medias':
            case 'project_documents':
            case 'project_property' :
            {
                $data = array_merge($projectRule, config('appSection-file.file'));
                break;
            }

            default :
            {
                $data = ['file_type' => ['required', function ($attribute, $value, $fail) {
                    $fail('тип файла не подходит');
                }]];
                break;
            }
        }
        return $data;
    }

    public function messages(): array
    {
        return [
            'file.required' => 'файл отсутствует',
            'file.file' => 'не является файлом',
            'file_type.required' => 'не указан тип файла',
            'file_type.in' => 'тип файла не подходит'
        ];
    }

    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return $this->check([
            'hasAccess',
        ]);
    }
}
